
let request_key = 'zs_award'
let skill_count_key = 'zs_award_count'
let skill_name = 'skill_name'
let skill_result = false

function rewrite_body(body) {
    if (body.message){
        body.message = "成功抓到请求，等待秒杀";
    }
    console.log(body);
}

function resp_case(request_value,count,res) {
    console.log('请求结果：');
    console.log(res.headers.date);
    console.log(res.data);
    if(res.data.code=='0000'){
        let skill_name = $store.get(skill_name);
        $feed.push("招行秒杀结果：", skill_name+res.data.message)
       
    }else if(res.data.message.includes("领光")){
        $feed.push("招行秒杀结果：", skill_name+"秒杀失败")
    }else{
        axios_skill(request_value,count)
    }
  
}
function error_case(request_value,count,e) {
    console.log(e.message)
    axios_skill(request_value,count)
}

//上面是暂时需要适配的部分
!(async () => {
    if (typeof $request !== "undefined") {
  console.log(`开始获取请求参数`);
   get_request_key();
   let body = $response.body;
    body =  JSON.parse(body);
    rewrite_body(body);
    body =  JSON.stringify(body);
    $done({ body })
}else {
    console.log(`开始执行请求`);
    let request_value = $store.get(request_key);
    if(request_value&&request_value!=""){
        //  $store.delete没有暴露出来，无法使用，兼容下
        // $store.set(request_key, "")  
        let count = $store.get(skill_count_key);
        if (!count){
            $store.set(skill_count_key, 1)  
            count = 1
        }
        setTimeout(axios_skill,700,request_value,count);
        setTimeout(axios_skill,900,request_value,count);
        // axios_skill(request_value,count);
    }else{
        console.log(request_key+`不存在`);
    }
    }
    })()
   .catch((e) =>console.log(e))
   .finally(() =>$done())

function axios_skill(request_value,count){
    if(count>0){
        console.log(`执行请求：`);
        count--;
        let req_url = request_value.url.split("=")[0]
        // let req_time = request_value.url.split("=")[1]
        // console.log(req_url);
        // console.log(req_time);
        var time1 = Date.parse( new Date() ).toString();
        time1 = time1.substr(0,13);
        //         console.log(time1);
        request_value.url = req_url +'='+time1;
        console.log(request_value.url);
        $axios(request_value)
        .then(res=>resp_case(request_value,count,res))
        .catch(e=>error_case(request_value,count,e));  
    }
  
}


function get_request_key() {
    $store.set(request_key, $request) 
}
